package com.mvp.shq.washer.ui.shop.category;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.clans.fab.FloatingActionMenu;
import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.MyViewPagerAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.Category;
import com.mvp.shq.washer.ui.main.MainActivity;
import com.mvp.shq.washer.ui.shop.categoryPage.CategoryFragment;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.JumpUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CategoryActivity extends BaseActivity<Presenter, Model> implements CategoryConstract.View {
    //tab 文字
    private String[] TAB_TITLES;// = new int[]{R.string.shop_category_xyy, R.string.shop_category_xyf, R.string.shop_category_xz};
    //Tab数量
    private int TAB_COUNT;// = TAB_TITLES.length;
    //tab 图片
    private ImageView[] TAB_IMGS;
    private MyViewPagerAdapter mAdapter;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    @BindView(R.id.menu)
    FloatingActionMenu menu;
    private Fragment[] TAB_FRAGMENTS;
    private List<Category> categories;
    private ImageUtil imageUtil;
    private int lastTabPosition = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_category;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("分类");
        imageUtil = new ImageUtil(context);
        showLoadingDialog(true);
        presenter.getCategory();
    }
    /**
     * @description: 设置添加Tab
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater,List<Category> list){
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.category_tab_custom,null);
            TextView tvTitle = (TextView)view.findViewById(R.id.tv_tab);
            tvTitle.setText(list.get(i).getCategoryName());
            ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
            TAB_IMGS[i] = imgTab;
            imageUtil.loadUrlImage(list.get(i).getIconUnselect(),imgTab);
            tab.setCustomView(view);
            tabLayout.addTab(tab);
        }
        if (list.size() > 0){
            imageUtil.loadUrlImage(categories.get(0).getIconSelect(),TAB_IMGS[0]);
        }
    }


    @OnClick({R.id.back, R.id.menu_item, R.id.menu_item1, R.id.menu_item2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.menu_item:
                finish();
                menu.close(true);
                break;
            case R.id.menu_item1:
                //购物车
                JumpUtil.toShopCar(context);
                break;
            case R.id.menu_item2:
                //个人中心
                JumpUtil.toShopUserCenter(context);
                menu.close(true);
                break;
        }
    }

    @Override
    public void getCategoryS(List<Category> list) {
        if (list == null){
            return;
        }
        categories = list;
        TAB_COUNT = categories.size();
        TAB_TITLES = new String[TAB_COUNT];
        TAB_FRAGMENTS = new Fragment[TAB_COUNT];
        TAB_IMGS = new ImageView[TAB_COUNT];
        for (int i=0;i<TAB_COUNT;i++){
            TAB_TITLES[i] = categories.get(i).getCategoryName();
            TAB_FRAGMENTS[i] = new CategoryFragment(categories.get(i).getId());
        }
        setTabs(tablayout, this.getLayoutInflater(), categories);
        //TAB_FRAGMENTS = new Fragment[]{new CategoryFragment("4ceaa161d6524ee6a5504bdef62fdd17"),new CategoryFragment("1cf9c3de3f334600b498e840256a16f4"),new CategoryFragment("5d86af3aeed041428c28772e177c16ba")};
        mAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        vpMain.setAdapter(mAdapter);
        vpMain.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
        tablayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vpMain));
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int index = tab.getPosition();
                if (index != lastTabPosition){
                    imageUtil.loadUrlImage(categories.get(index).getIconSelect(),TAB_IMGS[index]);
                    imageUtil.loadUrlImage(categories.get(lastTabPosition).getIconUnselect(),TAB_IMGS[lastTabPosition]);
                    lastTabPosition = index;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class MyViewPagerAdapter extends FragmentPagerAdapter {
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {
            return TAB_COUNT;
        }
    }
}
